var map;

function createMap()
{
  console.log('loaded');
//  var activeStationOverlays = [];
//   var pickerControl = new PointPickerControl(function(point, parentMap){
//       YMaps.jQuery.get('/svc/getNearestPoint/', {
//           lat: point.getLat(),
//           lng: point.getLng()
//       }, function(d){
//           if(activeStationOverlays.length > 0)
//           {
//               YMaps.jQuery.each(activeStationOverlays, function(){
//                   parentMap.removeOverlay(this);
//               });
//           }
//           YMaps.jQuery.each(d.response.stations, function(){
//               var station = this.station;
//               var ovl = new YMaps.Placemark(new YMaps.GeoPoint(station.point.lng, station.point.lat));
//               ovl.name = station.name;
//               ovl.description = "До остановки: " + this.distance + ' у.е.';
//               activeStationOverlays.push(ovl);
//               parentMap.addOverlay(ovl);
//           });
//       }, 'json');
//   });
  map = new YMaps.Map(YMaps.jQuery("#map-body")[0]);
  var geocoder = new YMaps.Geocoder("Ярославль");
  YMaps.Events.observe(geocoder, geocoder.Events.Load, function () 
  {
      if(!this.found)        
        map.setCenter(new YMaps.GeoPoint(37.64, 55.76), 10);    
      else
        map.setCenter(this.get(0).getCoordPoint(), 11, YMaps.MapType.MAP);
        
      map.addControl(new YMaps.TypeControl());
      map.addControl(new YMaps.Zoom());
      //map.addControl(pickerControl);
      drawRoutes();
      //console.log('found');
  });
}

var ovrStations = {};
var geoStations = {};
var ovrRoutes = {};

function drawRoutes()
{
  var styles = {};
  
  var style = new YMaps.Style();
  style.lineStyle = new YMaps.LineStyle();
  style.lineStyle.strokeColor = "FF0000";
  style.lineStyle.strokeWidth = "3";
  YMaps.Styles.add("tram_line", style);
  styles["tram_line"] = style;
  
  var style = new YMaps.Style();
  style.lineStyle = new YMaps.LineStyle();
  style.lineStyle.strokeColor = "0000FF";
  style.lineStyle.strokeWidth = "2";
  YMaps.Styles.add("trolley_line", style);
  styles["trolley_line"] = style;  
  
  var style = new YMaps.Style();
  style.lineStyle = new YMaps.LineStyle();
  style.lineStyle.strokeColor = "00FF00";
  style.lineStyle.strokeWidth = "3";
  YMaps.Styles.add("bus_line", style);
  styles["bus_line"] = style;  
  
   var style = new YMaps.Style();
   style.iconStyle = new YMaps.IconStyle();
   style.iconStyle.href = 'images/green.png';
   style.iconStyle.size = new YMaps.Point(7,7);   
   style.iconStyle.offset = new YMaps.Point(-4,-4);
   YMaps.Styles.add("bus_st", style);
   styles["bus_st"] = style;
  
   var style = new YMaps.Style();
   style.iconStyle = new YMaps.IconStyle();
   style.iconStyle.href = 'images/red.png';
   style.iconStyle.size = new YMaps.Point(7,7);   
   style.iconStyle.offset = new YMaps.Point(-4,-4);
   YMaps.Styles.add("tram_st", style);
   styles["tram_st"] = style;
  
   var style = new YMaps.Style();
   style.iconStyle = new YMaps.IconStyle();
   style.iconStyle.href = 'images/blue.png';
   style.iconStyle.size = new YMaps.Point(7,7);
   style.iconStyle.offset = new YMaps.Point(-4,-4);
   YMaps.Styles.add("trolley_st", style);
   styles["trolley_st"] = style;
  
  for(var i in info.routes)
  {
    var rt = info.routes[i];
    if(!rt.type)
      continue;

    for(var j=1; j<rt.stations.length; j++)
    {
      var id = "" + rt.stations[j] + ":" +rt.stations[j-1] + ":" + rt.type;
      if(ovrRoutes[id])
        continue;
        
      if(l1 = lines[rt.stations[j-1]])
        if(l2 = l1[rt.stations[j]])
        {      
          var line = YMaps.Polyline.fromEncodedPoints(l2[0], l2[1], {
            style: styles[rt.type + "_line"],          
            draggable: false,
            hasBalloon: false,
          });
          ovrRoutes[id] = line;        
          map.addOverlay(line);
        }  
    } 
    
    for(var j=0; j<rt.stations.length; j++)
      for(var k in info.stations)
        if(info.stations[k].id == rt.stations[j])
        {
          if(ovrStations[id])
            continue;          
            
          var st = info.stations[k];                               
          var geo = new YMaps.GeoPoint(st.lon, st.lat); 
          var ovr = new YMaps.Placemark(geo, {
            style: styles[rt.type + "_st"],
            draggable: false,
            hasBalloon: false,
          });
          
          YMaps.Events.observe(ovr, ovr.Events.Click, function(ovr) {
            showSingleRoute(rt.id);
            //console.log("clicked on", info.stations[k]);
            //alert(123);
          });
          
          geoStations[st.id] = geo;
          ovrStations[st.id] = ovr;
          map.addOverlay(ovr);
        }       
  }
}

function showSingleRoute()
{
  for(var i in ovrStations)
    map.removeOverlay(ovrStations[i]);
  
  for(var i in ovrRoutes)
    map.removeOverlay(ovrRoutes[i]);
}